package wk.demo.tool;

import android.app.Activity;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;


public class PermissionUtil {
    public static final int CODE_REQ_PERMISSION = 100;

    public static void requestPermission(Activity activity, String[] permissionArr, int requestCode) {
        if (permissionArr != null) {
            ActivityCompat.requestPermissions(activity, permissionArr, requestCode);
        }

    }

    public static void requestPermission(Activity activity, String permissionArr, int requestCode) {
        if (permissionArr != null) {
            ActivityCompat.requestPermissions(activity, new String[]{permissionArr}, requestCode);
        }
    }

    public static void requestPermissionIFNot(Activity activity, String permissionArr, int requestCode) {
        if (permissionArr != null && !isPermissionGranted(activity, permissionArr)) {
            requestPermission(activity, permissionArr, requestCode);
        }
    }


    public static boolean isPermissionGranted(Activity activity, String permission) {
        return ContextCompat.checkSelfPermission(activity, permission) == PackageManager.PERMISSION_GRANTED;
    }

}
